import { director } from "cc";

type SetIntervalFunction = (handler: () => void, timeout?: number, target?: any, ...args: any[]) => Function;
type ClearIntervalFunction = (handle: Function, target: any) => void;

interface IntervalProvider {
    setInterval: SetIntervalFunction;
    clearInterval: ClearIntervalFunction;
}

export const creatorIntervalProvider: IntervalProvider = {
    setInterval(...args) {
        let handler = args[0];
        let timeout = args[1];
        let target = args[2];
        let params = args.slice(3);
        let handle = () => handler.apply(this, params);
        director.getScheduler().schedule(handle, target ?? director.getScene(), timeout / 1000);
        return handle;
    },
    clearInterval(handle, target) {
        director.getScheduler().unschedule(handle, target ?? director.getScene());
    }
}